import COS from 'cos-js-sdk-v5'
import axios from 'axios';
// import {
//     Message
// } from 'element-ui'
export default function upload(file, callback) {

	var datas = {}
	let fileUrl = ''
	axios({
		url: 'https://zyk.icve.com.cn/hnhlkq/tm-openapi/app/file/getTxCosCredential',
		method: 'post',
		headers: {
			'Content-Type': 'application/json',
			'M-SESSION': localStorage.getItem('userUuid')
		},
		data: datas
	}).then((res) => {

		const data = res.data.data
		console.log(data, "9999999")
		let uploadFileName = Date.parse(new Date())
		var cos = new COS({
			getAuthorization: function(options, callback) {
				callback({
					TmpSecretId: data.credentials.tmpSecretId,
					TmpSecretKey: data.credentials.tmpSecretKey,
					XCosSecurityToken: data.credentials.sessionToken,
					StartTime: data.startTime,
					ExpiredTime: data.expiredTime,
					expiration: data.expiration,
					requestId: data.requestId
				})
			}
		})
		cos.putObject({
				Bucket: data.bucket_name, // 存储桶名称
				Region: 'ap-chengdu', // 地区
				Key: '/tweet/img/' + uploadFileName + '.png', // 图片名称
				StorageClass: 'STANDARD',
				Body: file, // 上传文件对象
				onHashProgress: function(progressData) {
					console.log('校验中', JSON.stringify(progressData));
				},
				onProgress: function(progressData) {
					console.log('上传中', JSON.stringify(progressData));
				},
			},
			function(err, data) {
				console.log('999', err, data, data.Location)
				if (err) {
					// Message({message:'文件上传失败,请重新上传',type: 'error',})
				} else {
					let fileUrl = 'http://' + data.Location
					callback(fileUrl)
				}
			}
		)
	})

}